from django.db import models


class ArticleCategory(models.Model):
    """
    文章分类模型
    """
    name = models.CharField(max_length=10)


class Article(models.Model):
    """
    博客文章模型
    """
    title = models.CharField(max_length=200)
    desc = models.CharField(max_length=200)
    thumbnail = models.URLField()
    content = models.TextField()
    pub_time = models.DateTimeField(auto_now_add=True)
    category = models.ForeignKey('ArticleCategory', on_delete=models.SET_NULL, null=True)
    author = models.ForeignKey('ynauth.User', on_delete=models.SET_NULL, null=True)
    delete = models.BooleanField(default=False)

    class Meta:
        ordering = ['-pub_time']


class Comment(models.Model):
    """
    评论模型
    """
    content = models.TextField()
    pub_time = models.DateTimeField(auto_now_add=True)
    article = models.ForeignKey("Article", on_delete=models.CASCADE, related_name='comments')
    author = models.ForeignKey("ynauth.User", on_delete=models.CASCADE)
    parent = models.ForeignKey(
        'self',
        on_delete=models.CASCADE,
        null=True,
        blank=True,
        related_name='children'
    )

    # 新增，记录二级评论回复给谁, str
    reply_to = models.ForeignKey(
        "ynauth.User",
        null=True,
        blank=True,
        on_delete=models.CASCADE,
        related_name='repliers'
    )

    class Meta:
        ordering = ['pub_time']

class Banner(models.Model):
    """
    主页轮播图模型
    """
    priority = models.IntegerField(default=0)
    image_url = models.URLField()
    link_to = models.URLField()
    pub_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['priority']

class Advertisement(models.Model):
    """
    侧边栏广告模型
    """
    image_url = models.URLField()
    link_to = models.URLField()
    pub_time = models.DateTimeField(auto_now_add=True)
